package com.hymall.service.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.bean.*;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.CommonData;
import com.hymall.bean.common.User;
import com.hymall.bean.vo.AdminOrderChannelVo;
import com.hymall.mapper.MarketCommentMapper;
import com.hymall.mapper.MarketOrderGoodsMapper;
import com.hymall.mapper.MarketOrderMapper;
import com.hymall.mapper.MarketUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author: 牟治宇
 * @description 商场订单实现类
 * @date: 2022/7/18 11:40
 */
@Service
public class

OrderServiceImpl implements OrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;




    @Override
    public Map<String, Object> queryOrderDetail(Integer id) {
        Map map = new HashMap<>();
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);
        MarketUser user = marketUserMapper.selectByPrimaryKey(marketOrder.getUserId());
        HashMap<String, Object> usermap = new HashMap<>();
        usermap.put("nickname", user.getNickname());
        usermap.put("avatar", user.getAvatar());

        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        marketOrderGoodsExample.createCriteria().andOrderIdEqualTo(marketOrder.getId());
        List<MarketOrderGoods> marketOrderGoodsList = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);

        map.put("orderGoods", marketOrderGoodsList);
        map.put("user", usermap);
        map.put("order", marketOrder);
        return map;
    }

    @Override
    public int orderRefund(BigDecimal refundMoney, Integer orderId) {
        BigDecimal actualPrice = marketOrderMapper.selectActualPrice(orderId);
        if (refundMoney.compareTo(actualPrice) == 0) {
            int code = marketOrderMapper.orderRefund(orderId);
            return code;
        }
        return 0;
    }

    @Override
    public CommonData getOrderList(Date start, Date end, Short[] orderStatusArray,
                                   Integer id, Integer oid, String orderSn, BasePageInfo basePageInfo) {

        PageHelper.startPage(basePageInfo.getPage(),basePageInfo.getLimit());
        PageHelper.orderBy("update_time desc");

        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = orderExample.createCriteria();
        if (id != null) {
            criteria.andUserIdEqualTo(id);
        }
        if (orderSn != null) {
            criteria.andOrderSnLike("%" + orderSn + "%");
        }
        if (orderStatusArray != null) {
            List<Short> list = Arrays.asList(orderStatusArray);
            criteria.andOrderStatusIn(list);
        }
        if (start != null && end != null) {
            // List<Date> dates = Arrays.asList(timeArray);
            criteria.andAddTimeBetween(start, end);
        }

        List<MarketOrder> orderList = marketOrderMapper.selectByExample(orderExample);

        PageInfo<MarketOrder> marketOrderPageInfo = new PageInfo<>(orderList);

        return CommonData.data(marketOrderPageInfo);
    }

    @Override
    public List<AdminOrderChannelVo> getChannel() {
        List<AdminOrderChannelVo> list = marketOrderMapper.selectChannel();
        return list;
    }


    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Override
    public int insertComment(String commentId, String content) {
        // MarketCommentExample marketCommentExample = new MarketCommentExample();
        int id = Integer.parseInt(commentId);
        // marketCommentExample.createCriteria().andIdEqualTo(id);

        MarketComment marketComment = marketCommentMapper.selectByPrimaryKey(id);
        if (marketComment.getAdminContent()!= null && !("").equals(marketComment.getAdminContent())){
            return -1;
        }
        // marketComment.setAdminContent(content);
        // marketCommentMapper.deleteByPrimaryKey(id);
        marketCommentMapper.updateComment(id,content);

     return 0;
    }


}
